/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

#ifndef EXAMPLES_REDUCE_SUM_CUSTOM_IMPL_H
#define EXAMPLES_REDUCE_SUM_CUSTOM_IMPL_H

#include "../op_host/sum_custom_tiling.h"
#include "kernel_operator.h"

namespace MyCustomKernel {
template <typename T>
class KernelSum {
public:
    __aicore__ inline KernelSum() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, SumCustomTilingData tilingData, AscendC::TPipe *pipeIn) {
        ASCENDC_ASSERT(AscendC::GetBlockNum() != 0, { KERNEL_LOG(KERNEL_ERROR, "block dim can not be zero!"); });
        inner = tilingData.inner;
        outter = tilingData.outter;
        n = tilingData.n;
        tmpBufSize = tilingData.tmpBufSize;
        out_inner = tilingData.out_inner;

        params.inner = inner;
        params.outter = outter;
        params.n = n;

        xGm.SetGlobalBuffer((__gm__ T *)x);
        yGm.SetGlobalBuffer((__gm__ T *)y);

        pipe = pipeIn;
        pipe->InitBuffer(inQueue, 1, inner * outter * sizeof(T));
        pipe->InitBuffer(outQueue, 1, out_inner * sizeof(T));
        pipe->InitBuffer(tmpBuf, tmpBufSize * sizeof(uint8_t));
    }
    __aicore__ inline void Process() {
        CopyIn();
        Compute();
        CopyOut();
    }

private:
    __aicore__ inline void CopyIn() {
        AscendC::LocalTensor<T> xLocal = inQueue.AllocTensor<T>();
        AscendC::DataCopy(xLocal, xGm, inner * outter);
        inQueue.EnQue(xLocal);
    }
    __aicore__ inline void Compute() {
        AscendC::LocalTensor<T> xLocal = inQueue.DeQue<T>();
        AscendC::LocalTensor<T> yLocal = outQueue.AllocTensor<T>();
        AscendC::LocalTensor<uint8_t> sharedTmpBuffer = tmpBuf.AllocTensor<uint8_t>();

        T scalar(0);
        AscendC::Duplicate<T>(yLocal, scalar, out_inner);
        AscendC::Sum(yLocal, xLocal, sharedTmpBuffer, params);

        outQueue.EnQue<T>(yLocal);
        inQueue.FreeTensor(xLocal);
        tmpBuf.FreeTensor(sharedTmpBuffer);
    }
    __aicore__ inline void CopyOut() {
        AscendC::LocalTensor<T> yLocal = outQueue.DeQue<T>();
        AscendC::DataCopy(yGm, yLocal, out_inner);
        outQueue.FreeTensor(yLocal);
    }

private:
    AscendC::TPipe* pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueue;
    AscendC::TQue<AscendC::TPosition::VECOUT, 1> outQueue;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf;
    AscendC::GlobalTensor<T> xGm;
    AscendC::GlobalTensor<T> yGm;

    uint32_t inner = 0;
    uint32_t outter = 0;
    uint32_t n = 0;
    uint32_t tmpBufSize = 0;
    uint32_t out_inner = 0;
    AscendC::SumParams params;
};
}

#endif